package keeto.com.vn.classified.adapter;

import java.util.ArrayList;

import keeto.com.vn.classified.ui.MainApplication;
import keeto.com.vn.classified.ui.R;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.androidquery.AQuery;


public class ViewPagerAdapterProduct extends PagerAdapter {
	private Context context;
	private ArrayList<String> mArrayList;
	private ImageView iv_img_product_adapter;
	private TextView tv_discount;
	private String discount;
	int iwith;
	private LayoutParams  layoutParams;
private AQuery query;
	public ViewPagerAdapterProduct(Context context, ArrayList<String> listImage, String discount, AQuery mQuery) {
		this.context = context;
		this.mArrayList =listImage;
		this.discount = discount;
		iwith = (int)(MainApplication.getScreenWidth()*5/8);
		this.layoutParams = new RelativeLayout.LayoutParams(iwith, iwith);
		this.query = mQuery;
	}


	@Override
	public int getCount() {
		return mArrayList.size();
	}
	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		return arg0 == ((View) arg1);
	}
	@Override
	public void destroyItem(View arg0, int arg1, Object arg2) {
		((ViewPager) arg0).removeView((View) arg2);
	}

	@Override
	public Parcelable saveState() {
		return null;
	}
	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View rowview = inflater.inflate(R.layout.row_adapter_product, null);
		iv_img_product_adapter = (ImageView) rowview.findViewById(R.id.img_product_adapter);
		tv_discount = (TextView) rowview.findViewById(R.id.tv_discount);

		iv_img_product_adapter.setLayoutParams(layoutParams);
		if (mArrayList.get(position) != null && !mArrayList.get(position).equals("")){
			query.id(iv_img_product_adapter).image(mArrayList.get(position), true, true, 0, 0, null, 0, 1.0f);

		}else{
			query.id(iv_img_product_adapter).image(R.drawable.no_image);
		}
//		iv_img_product_adapter.setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Intent intent = new Intent(context, ImgProductFullScreen.class);
//				context.startActivity(intent);
//			}
//		});
		if (discount != null && !discount.equals("") && !discount.equals("null")){
			tv_discount.setText("-" + discount + "%");
			tv_discount.setVisibility(View.VISIBLE);
		}else{
			tv_discount.setVisibility(View.GONE);
		}

		((ViewPager) container).addView(rowview, 0);
		return rowview;
	}



}
